package com.ej.busi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ej.busi.domain.EjGrade;
import com.ej.busi.dto.grade.EjGradeDetailDto;
import com.ej.busi.dto.grade.EjGradeDto;
import com.ej.busi.mapper.EjGradeMapper;
import com.ej.busi.service.IEjGradeService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课程表 服务层实现
 * 
 * @author ej
 */
@Service
public class EjGradeServiceImpl extends ServiceImpl<EjGradeMapper, EjGrade> implements IEjGradeService {

    @Override
    public List<EjGradeDto> getStageAndGrade() {
        List<EjGradeDto> result = Lists.newArrayList();
        // 获取所有列表
        List<EjGrade> list = this.list();
        Map<Long, List<EjGrade>> collect = list.stream().collect(Collectors.groupingBy(EjGrade::getSorted));
        collect.keySet().forEach( s -> {
            EjGradeDto dto = new EjGradeDto();
            dto.setSorted(s);
            dto.setGradeType(collect.get(s).get(0).getGradeType());
            List<EjGrade> ejGrades = collect.get(s);
            List<EjGradeDetailDto> gradeDetailDtos = Lists.newArrayList();
            ejGrades.forEach( e -> {
                EjGradeDetailDto de = new EjGradeDetailDto();
                de.setId(e.getId());
                de.setGrade(e.getName());
                gradeDetailDtos.add(de);
            });
            dto.setGradeDetailDtos(gradeDetailDtos.stream().sorted(Comparator.comparing(EjGradeDetailDto :: getId)).collect(Collectors.toList()));
            result.add(dto);
        });

        return result.stream().sorted(Comparator.comparing(EjGradeDto :: getSorted)).collect(Collectors.toList());
    }
}
